<template>
<!-- 频道弹出层 -->
  <div >
    <!-- 已选频道(已选) -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span>已添加频道</span>
        <span class="small_title">{{isShow?'点击删除频道':'点击进入频道'}}</span>
        <span @click="isShow=!isShow">{{isShow?'完成':'编辑'}}</span>
      </div>
      <!-- 已选频道列表 -->
      <van-row type="flex">
        <van-col
          span="6"
          v-for="(obj,index) in channelList"
          :key="obj.id"
          @click="removeChannelFn(obj.id,index)"
        >
          <div class="channel-item van-hairline--surround">
            {{obj.name}}
            <!-- 删除的徽标 -->
            <van-badge
              color="transparent"
              class="cross-badge"
              v-show="isShow &&obj.id!=0"
            >
              <template #content>
                <van-icon
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="12"
                />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道(未选) -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="obj in unChannelList" :key="obj.id"

        @click="addChannelFn(obj)">
          <div class="channel-item van-hairline--surround">
            {{obj.name}}
          </div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import { updateChannelAPI } from '@/api'
export default {
  props: ['channelList', 'unChannelList', 'value'],
  // 22.3点击徽标
  data () {
    return {
      isShow: false
    }
  },
  methods: {
    // 删除频道--刷新后还有，所以调用接口，传给后台也删除
    async removeChannelFn (id, index) {
      // 推荐频道不能删
      if (id === 0) return
      if (this.isShow === true && this.channelList.length > 2) { // 必须是编辑状态,才能删除
      // 删除本地数组数据
        const index = this.channelList.findIndex(obj => obj.id === id)
        this.channelList.splice(index, 1)

        this.updateChannelFn()
      } else { // 22.7切换频道
        this.$emit('input', index) // 把索引传给父
        this.$emit('closePop') // 父把弹出层关闭
      }
    },

    // 22.5先删除后添加都需要调用，所以封装更新方法
    async updateChannelFn () {
      // 推荐不要发给后台(后台要求的，否则后台报错400)
      let updateArr = this.channelList.filter(obj => obj.id !== 0)
      // 后台要的参数不是id和name，而是id和seq(序号+1)
      updateArr = updateArr.map((obj, index) => ({ id: obj.id, seq: index + 1 }))
      // 立刻马上调用更新接口
      await updateChannelAPI({ channels: updateArr })
    },

    // 22.5新增频道方法
    addChannelFn (obj) {
      // 因为channelList跟index.vue里的变量是同一个，index.vue的计算属性会重新给unChannelList计算新的值
      // 所以这个地方不需要处理unChannelList
      this.channelList.push(obj)
      this.updateChannelFn()
    }
  }
}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small_title {
  font-size: 12px;
  color: gray;
}
</style>
